package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.TianJieFinanceAccountDto;
import org.itboys.mobile.service.mongo.mobile.TianJieMobileAccountService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_消费卡_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/account")
public class TianJieMobileAccountController extends BaseController{
    @Autowired
    private TianJieMobileAccountService accountService;

    @RequestMapping(value="/getAccount",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【提现-提现账户】获取我的提现账户(不分页)")
    public void getAccount(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();

            List<TianJieFinanceAccountDto> accounts=accountService.getAccount(memberId);
            ResponseMessage.success(accounts);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/account/getAccount----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /*@RequestMapping(value="/check",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【提现-提现账户】校验提现账户是否有效，新增或编辑提现账户前调用。校验成功后会返回对应的提现账户信息")
    public void check(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="accountNo", value="银行账号号码")@RequestParam(value = "accountNo", required = true) String accountNo,
                    @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = false) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = false) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            Map<String,Object> result= accountService.check(accountNo);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/account/check----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }*/
    @RequestMapping(value="/save",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【提现-提现账户】新增/或修改提现账户。")
    public void save(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(required=true, name="accountId", value="账户ID.新增传0")@RequestParam(value = "accountId", required = true) Long accountId,
                          @ApiParam(required=true, name="btAccount", value="小比特币地址")@RequestParam(value = "btAccount", required = true) String btAccount,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = false) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = false) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            accountService.saveAccount( accountId, memberId, btAccount);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/account/svae----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/delete",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【提现-提现账户】删除提现账户")
    public void delete(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="accountId", value="账户ID.新增传0")@RequestParam(value = "accountId", required = true) Long accountId,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = false) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = false) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            accountService.deleteAccount( accountId, memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/account/delete----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
